<template>
  <view>
    <view class="search-box">
      <!-- <uni-icons type="search" size="16"></uni-icons> -->
      <input ref="input" focus="true" @input="input" type="text" placeholder="请输入搜索内容" />
    </view>
    <view class="sugg-list" v-if="kw !== ''" >
      <view class="sugg-item" v-for="item in searchList" :key="item.goods_id" @click="gotoDeatail(item.goods_id)">
        <view class="sugg-txt">
          {{item.goods_name}}
        </view>
        <uni-icons type="arrowright" size="14"></uni-icons>
      </view>
    </view>
    <view class="sugg-his-box" v-else>
      <view class="sugg-his">
        <text>搜索历史</text>
        <uni-icons type="trash" size="17" @click="clean"></uni-icons>
      </view>
      <view class="sugg-his-content">
        <view class="sugg-his-item" v-for="item in searchHis" :key="item" @click="gotoHis(item)">
          <text>{{item}}</text>
        </view>
      </view>
    </view>
  </view>
</template>

<script>
  import $http from '../../utils/request';
  export default {
    data() {
      return {
        timer: null,
        kw: '',
        searchList: [],
        searchHis: []
      };
    },
    methods: {
      // 获取用户输入数据并节流
      input(e) {
        clearTimeout(this.timer)
        this.timer = setTimeout(() => {
          this.kw = e.target.value
          this.getSearchList()
        },500)
      },
      //获取搜索列表数据
      async getSearchList() {
        if (this.kw === '') {
          return
        }
        // this.searchHis = this.searchHis.filter(item => item !== this.kw)
        // this.searchHis.unshift(this.kw)
        this.searchHis = [...new Set([this.kw, ...this.searchHis])]
        // uni.SetStorageSync('his', JSON.stringify(this.searchHis))
        uni.setStorageSync('his', JSON.stringify(this.searchHis))
        const res = await uni.$http.get('api/public/v1/goods/qsearch',{query: this.kw})
        // console.log(res.data.message);
        this.searchList = res.data.message
      },
      // 跳转
      gotoDeatail(id) {
        uni.navigateTo({
          url: '/subpkg/good_detail/good_detail?goods_id=' + id
        })
      },
      // 删除
      clean() {
        this.searchHis = []
        uni.setStorageSync('his', '[]')
      },
      // 点击搜索历史
      gotoHis(item) {
        uni.navigateTo({
          url: '/subpkg/goods_list/goods_list?query=' + item
        })
      }
    },
    onLoad() {
      this.searchHis = JSON.parse(uni.getStorageSync('his') || '[]') 
    }
  }
</script>

<style lang="scss">
  .search-box {
    position: sticky;
    top: 0;
    z-index: 999;
    height: 40px;
    line-height: 40px;
    padding: 10px;
    background-color: #C00000;

    input {
      height: 100%;
      background-color: #fff;
      border-radius: 20px;
      padding-left: 20px;
    }
  }
  
  .sugg-item{
    padding: 13px 10px;
    display: flex;
    border-bottom: 1px solid #efefef;
    .sugg-txt{
      flex: 1;
      font-size: 14px;
      overflow: hidden;
      white-space: nowrap;
      text-overflow: ellipsis;
    }
  }
  .sugg-his {
    display: flex;
    justify-content: space-between;
    padding: 10px;
    margin-bottom: 10px;
    border-bottom: 1px solid #e8e8e8;
  }
  .sugg-his-content{
    display: flex;
    flex-wrap: wrap;
      padding: 0 10px;
      .sugg-his-item {
        margin-right: 10px;
        margin-bottom: 20px;
      text {
        padding: 5px 10px;
        font-size: 12px;
        background-color: #ebebeb;
        border-radius: 20px;
      }
    }
  }
</style>